﻿using AppDataStruct;
using ComLib.ComType;
using ComUIControl;
using KWindow.KPageCore;
using KXmlUIControl.KControl;
using LogLib;
using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace YSLPages
{
    public partial class PageApplyTWZInfo : PageControlBase
    {      
        private ApplyServiceType _serviceType = ApplyServiceType.ApplyNewAndQZ;
        private PreApplyInfo _appInfo = null;
        private KCheckBox kCheckBoxYZZQZ = null;
        private KCheckBox kCheckBoxSXCXSQ = null;
        private KCheckBox kCheckBoxSCSQ = null;
        private KCheckBox kCheckBoxBF = null;
        private KCheckBox kCheckBoxHF = null;

        private TextBox textBoxTWZHM = null;
        private TextBox textBoxValidDate = null;

        private Button buttonOK = null;
        private Button buttonCancell = null;

        private List<KCheckBox> listTWSQLBCheckBoxs = new List<KCheckBox>();

        private List<KCheckBox> listTWYWLBCheckBoxs = new List<KCheckBox>();

        public PageApplyTWZInfo()
        {
            InitializeComponent();
            SetProcessBase(ProcessApplyTWZInfo.Instance);
        }

        private void GetControls()
        {
            kCheckBoxYZZQZ = GetControl<KCheckBox>("kCheckBoxYZZQZ") as KCheckBox;
            textBoxTWZHM = GetControl<TextBox>("textBoxTWZHM") as TextBox;
            textBoxValidDate = GetControl<TextBox>("textBoxValidDate") as TextBox;
            kCheckBoxSXCXSQ = GetControl<KCheckBox>("kCheckBoxSXCXSQ") as KCheckBox;
            kCheckBoxSCSQ = GetControl<KCheckBox>("kCheckBoxSCSQ") as KCheckBox;
            kCheckBoxBF = GetControl<KCheckBox>("kCheckBoxBF") as KCheckBox;
            kCheckBoxHF = GetControl<KCheckBox>("kCheckBoxHF") as KCheckBox;
            buttonOK = GetControl<Button>("buttonOK") as Button;
            buttonCancell = GetControl<Button>("buttonCancell") as Button;
        }

        //初始化窗体数据
        public override void InitCtrls()
        {
            InitKeyboards();
            base.InitCtrls();
            GetControls();
            GetGroupCheckBoxs();           
            SetButtonDefaultStyles(buttonOK);
            SetButtonDefaultStyles(buttonCancell);
        }

        //初始化窗体数据
        public override void InitData()
        {
            base.InitData();
            if (Process.ContainsKey("DLZServiceType"))
            {
                _serviceType = Process.GetValue<ApplyServiceType>("DLZServiceType");
            }
            else
            {
                _serviceType = ApplyServiceType.ApplyNewAndQZ;
            }
            _appInfo = Process.GetValue<PreApplyInfo>("PreApplyInfo");
            if(_appInfo == null)
            {
                _appInfo = new PreApplyInfo();
            }
            InitTextBoxs();
            InitCheckBoxs();
            //_bInited = true;
        }

        private void GetGroupCheckBoxs()
        {
            List<Control> list0 = dicRootGroup["groupBZLB"];
            foreach (Control ctl in list0)
            {
                if (ctl is KCheckBox)
                {
                    //if (ctl.Name == "kCheckBoxSCSQ" && !string.IsNullOrEmpty(_appInfo.DLZSQ.ZJHM))
                    //{
                    //    ctl.Visible = false;
                    //}
                    listTWSQLBCheckBoxs.Add(ctl as KCheckBox);
                }
            }
            List<Control> list1 = dicRootGroup["groupYWLB"];
            foreach (Control ctl in list1)
            {
                if (ctl is KCheckBox)
                {
                    listTWYWLBCheckBoxs.Add(ctl as KCheckBox);
                }
            }
        }

        private void UpdateServiceType()
        {
            int index = listTWYWLBCheckBoxs.FindIndex(r=>r.Checked);
            if(index >= 0)
            {
                int code = int.Parse((string)(listTWYWLBCheckBoxs[index].Tag));
                _serviceType = (ApplyServiceType)code;
            }
        }

        private bool CheckUserCheckedIsOK()
        {
            int count = 0;
            foreach (KCheckBox box in listTWYWLBCheckBoxs)
            {
                if (box.Checked)
                {
                    count++;
                }
            }           
            if (count == 0)
            {
                MsgBox.Show("请选择办证类别.");
                return false;
            }
            else if(count > 1)
            {
                MsgBox.Show("系统错误: 选择了多个业务类别.");
                return false;
            }

            // 需要办理新证的
            if(!kCheckBoxYZZQZ.Checked)
            {
                count = 0;
                foreach (KCheckBox box in listTWSQLBCheckBoxs)
                {
                    if (box.Checked)
                    {
                        count++;
                    }
                }
                if (count == 0)
                {
                    MsgBox.Show("请选择办证类别.");
                    return false;
                }
                else if (count > 1)
                {
                    MsgBox.Show("系统错误: 选择了多个办证类别.");
                    return false;
                }
            }           
          
            if (kCheckBoxBF.Checked
                || kCheckBoxSXCXSQ.Checked
                || kCheckBoxHF.Checked)
            {
                if (string.IsNullOrEmpty(textBoxTWZHM.Text)
                    || string.IsNullOrEmpty(textBoxValidDate.Text))
                {
                    MsgBox.Show("请填写历史证件信息.");
                    return false;
                }
            }

            string todayDate = DateTime.Now.ToString("yyyyMMdd");
            string yxq = textBoxValidDate.Text;
            if (kCheckBoxSXCXSQ.Checked)
            {   
                if (yxq.CompareTo(todayDate) > 0)
                {
                    MsgBox.Show("您的证件在有效期内，不能办理过期申领，请选择补发！");
                    return false;
                }
            }
            if (kCheckBoxBF.Checked)
            {
                if (yxq.CompareTo(todayDate) < 0)
                {
                    MsgBox.Show("您的证件不在有效期内，不能办理补发，请选择过期申领！");
                    return false;
                }
            }

            if (kCheckBoxSCSQ.Checked && !string.IsNullOrEmpty(_appInfo.DLZSQ.ZJHM))
            {
                MsgBox.Show("已经办理过台湾通行证，不能选择首次申请!");
                return false;
            }

            return true;
        }

        private void UpdateBZLBInfo()
        {
            foreach (KCheckBox box in listTWSQLBCheckBoxs)
            {
                if (box.Checked)
                {
                    _appInfo.DLZSQ.YWLB.Code = (string)box.Tag;
                    _appInfo.DLZSQ.YWLB.Value = box.Title;
                    break;
                }
            }     
            if (_appInfo.DLZSQ.YWLB.Code != "11")
            {
                _appInfo.DLZSQ.ZJHM = textBoxTWZHM.Text;
                _appInfo.DLZSQ.ZJYXQZ = textBoxValidDate.Text;
            }
        }

        protected void InitCheckBoxs()
        {
            string ywlb = ((int)(_serviceType)).ToString();
            int index = listTWYWLBCheckBoxs.FindIndex(r => ywlb == (string)(r.Tag));
            if (index < 0)
            {
                index = 0;
            }
            for (int i = 0; i < listTWYWLBCheckBoxs.Count; i++)
            {
                listTWYWLBCheckBoxs[i].Checked = (i == index);
            }
            
            index = 0;
            if(_appInfo != null)
            {
                if(string.IsNullOrEmpty(_appInfo.DLZSQ.YWLB.Code))
                {
                    _appInfo.DLZSQ.YWLB.Code = CheckBZLBCode();
                }
                index = listTWSQLBCheckBoxs.FindIndex(r => _appInfo.DLZSQ.YWLB.Code == (string)r.Tag);
                if (index < 0)
                {
                    index = 0;
                }
            }           
            for (int i = 0; i < listTWSQLBCheckBoxs.Count; i++)
            {
                listTWSQLBCheckBoxs[i].Checked = (i == index);
            }
        }

        protected void InitTextBoxs()
        {
            if (_appInfo == null)
            {
                return;
            }
            if (!string.IsNullOrEmpty(_appInfo.DLZSQ.ZJHM))
            {
                textBoxTWZHM.Text = _appInfo.DLZSQ.ZJHM;
                //kCheckBoxSCSQ.Visible = false;
            }
            if (!string.IsNullOrEmpty(_appInfo.DLZSQ.ZJYXQZ))
            {
                textBoxValidDate.Text = _appInfo.DLZSQ.ZJYXQZ;
            }            
            
        }

        private string CheckBZLBCode()
        {
            if (string.IsNullOrEmpty(_appInfo.DLZSQ.ZJYXQZ))
            {
                return "11";  // 首次申领
            }
            string zjyqx = _appInfo.DLZSQ.ZJYXQZ;
            string dateNow = DateTime.Now.ToString("yyyyMMdd");
           
            // 还没有过期
            if(dateNow.CompareTo(zjyqx) < 0)
            {
                return "21";
            }
            else // 过期重领
            {
                return "13";
            }
        }


        /// <summary>
        /// 保存窗体数据(窗体本次做完后, 进入下一步之前)
        /// </summary>
        public override void UpdateData()
        {
            UpdateServiceType();
            Process.SetValue("DLZServiceType", _serviceType);
            if(!kCheckBoxYZZQZ.Checked)
            {
                UpdateBZLBInfo();
            }           
            Process.SetValue("DLZChecked", true);
            Process.SetValue("PreApplyInfo", _appInfo);          
        }

        /// <summary>
        /// 清理窗体数据(窗体本次做完后)
        /// </summary>
        public override void ClearData()
        {
            base.ClearData();
            for (int i = 0; i < listTWSQLBCheckBoxs.Count; i++)
            {
                listTWSQLBCheckBoxs[i].Checked = false;
            }
            for(int i = 0; i < listTWYWLBCheckBoxs.Count; i++)
            {
                listTWYWLBCheckBoxs[i].Checked = false;
            }
            textBoxTWZHM.Text = "";
            textBoxValidDate.Text = "";
        }

        /// <summary>
        /// 窗体关闭时
        /// </summary>
        public override void Close()
        {
            base.Close();
        }

        public override void StartWork()
        {
            base.StartWork();
        }

        public override void StopWork()
        {
            base.StopWork();
        }

        public override void OnCountdownCompleted(object sender, EventArgs e)
        {
            Log.WriteAsync("用户操作超时，返回首页");
            base.OnCountdownCompleted(sender, e);
        }

        protected override void btnBack_Click(object sender, EventArgs e)
        {
            base.btnBack_Click(sender, e);
        }

        protected override void btnNext_Click(object sender, EventArgs e)
        {
            if (!CheckUserCheckedIsOK())
            {
                return;
            }
            StopCountdown();
            UpdateBZLBInfo();
            UpdateData();
            ClearData();
            Process.ToAny("YSLPages.PageSelectDocumentType");
        }

        protected override void btnCancel_Click(object sender, EventArgs e)
        {
            base.btnCancel_Click(sender, e);
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (!CheckUserCheckedIsOK())
            {
                return;
            }
            StopCountdown();
            UpdateData();
            switch (_serviceType)
            {
                case ApplyServiceType.ApplyNewAndQZ:
                    Process.ToAny("YSLPages.PageApplyTWQZ");
                    break;
                case ApplyServiceType.OnlyApplyNew:
                    Process.ToAny("YSLPages.PageSelectDocumentType");
                    break;
                case ApplyServiceType.OnlyQZ:
                    Process.ToAny("YSLPages.PageApplyTWQZ");
                    break;
                default:
                    break;
            }            
            ClearData();
        }

        private void btnCancell_Click(object sender, EventArgs e)
        {
            Process.SetValue("DLZChecked", false);
            if(_appInfo != null)
            {
                _appInfo.DLZSQ.Reset();
            }
            StopCountdown();
            ClearData();
            Process.ToAny("YSLPages.PageSelectDocumentType");
        }
    }
}
